﻿#region usings
using System;
using System.Collections.Generic;
using System.Linq;
using InsuranceAdjuster.Services;
using InsuranceAdjuster.Web.Models;
#endregion
namespace InsuranceAdjuster.OccasionallyConnected.Messages {
    public class GetAppointmentsMessage : Message {

        public GetAppointmentsMessage() {
            CacheRules = MessageDataSource.PreferServerOverCache;
        }

        /// <summary>
        /// Gets called when Online 
        /// </summary>
        protected override void OnlineOperation() {
            MarkAsyncStarted();

            // Get appointments using RIA Services
            GetDataService().GetAppointments(appointments => {
                
                    for (int x = 0; x < appointments.Count; x++) {
                        this.ResponseEntities.Add(x.ToString(), 
                                                                        new MessageResult(Verb.Read, 
                                                                                                            Origin.Server, 
                                                                                                            appointments[x]));
                    }
                    MarkAsyncFinished();
                });
        }

        protected override List<MessageResult> OfflineOperation(IObjectStore objectStore) {
            return objectStore.GetEntities<AdjusterAppointment>().
                Select(x => new MessageResult(Verb.Read, 
                                                                        Origin.Cache, 
                                                                        x))
                           .ToList();
        }

    }
}
